package com.aier.proxy.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.proxy.entity.VideoDevice;

public class VideoDeviceRowMapper implements RowMapper<VideoDevice> {

    @SuppressWarnings("unused")
    private int type;

    public VideoDeviceRowMapper() {
    }

    public VideoDeviceRowMapper(int type) {
        this.type = type;
    }

    @Override
    public VideoDevice mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseVideoDevice(new VideoDevice(), rs);
    }

    public static VideoDevice parseVideoDevice(VideoDevice t, ResultSet rs) throws SQLException {
        return parseVideoDevice(t, rs, null);
    }

    public static VideoDevice parseVideoDevice(VideoDevice t, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            t.setVid(rs.getLong(su+"vid"));
        } catch (SQLException e) {
            t.setVid(null);
        }
        try {
            t.setSn(rs.getString(su+"sn"));
        } catch (SQLException e) {
            t.setSn(null);
        }

        try {
            t.setState(rs.getShort(su+"state"));
        } catch (SQLException e) {
            t.setState(null);
        }

        try {
            t.setUser(rs.getString(su+"user"));
        } catch (SQLException e) {
            t.setUser(null);
        }
        try {
            t.setPwd(rs.getString(su+"pwd"));
        } catch (SQLException e) {
            t.setPwd(null);
        }
        try {
            t.setUrl(rs.getString(su+"url"));
        } catch (SQLException e) {
            t.setUrl(null);
        }
        
        return t;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, VideoDevice t) {
        par.put("vid", t.getVid());
        par.put("sn", t.getSn());
        par.put("state", t.getState());
        par.put("user", t.getUser());
        par.put("pwd", t.getPwd());
        par.put("url", t.getUrl());

        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, VideoDevice t) {
        args.put("vid", t.getVid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final VideoDevice t) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(t),
                        new String[] { "vid" });
                parsePS(ps, t);
                return ps;
            }

        };
    }

    private static String parseSQL(VideoDevice t) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into VideoDevice(");
        int count = 0;
        if (null != t.getState()) {
            sql.append("state,");
            count++;
        }

        if (null != t.getVid()) {
            sql.append("vid,");
            count++;
        }
        
        if (null != t.getPwd()) {
            sql.append("pwd,");
            count++;
        }
        
        if (null != t.getSn()) {
            sql.append("sn,");
            count++;
        }
        
        if (null != t.getUrl()) {
            sql.append("url,");
            count++;
        }
        
        if (null != t.getUser()) {
            sql.append("user,");
            count++;
        }

        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, VideoDevice t)
        throws SQLException {
        int count = 1;
        if (null != t.getState()) {
            ps.setLong(count++,t.getState());
        }
        
        if (null != t.getVid()) {
            ps.setLong(count++,t.getVid());
        }
        if (null != t.getPwd()) {
            ps.setString(count++,t.getPwd());
        }
        if (null != t.getSn()) {
            ps.setString(count++,t.getSn());
        }
        if (null != t.getUrl()) {
            ps.setString(count++,t.getUrl());
        }
        if (null != t.getUser()) {
            ps.setString(count++,t.getUser());
        }

    }
}
